using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
//using System.Web.UI.WebControls.Image;

/// <summary>
/// Summary description for ImageHelp
/// </summary>
public class ImageHelperExt
{
    public static int MAX_THUMBNAIL_WIDTH;
    public static int MAX_THUMBNAIL_HEIGHT;

    public ImageHelperExt()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    //public static void Crop(string img, int width, int height, int x, int y, string des)
    //{
    //    Image image = Image.FromFile(img);
    //    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    //    bmp.SetResolution(80, 60);

    //    Graphics gfx = Graphics.FromImage(bmp);
    //    gfx.SmoothingMode = SmoothingMode.HighQuality;
    //    gfx.CompositingQuality = CompositingQuality.HighQuality;
    //    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    //    gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
    //    gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
    //    bmp.Save(des);
    //    // Dispose to free up resources
    //    bmp.Dispose();
    //    image.Dispose();
    //    gfx.Dispose();
    //}

    public static void Crop(string img, int width, int height, string des)
    {
        Image image = Image.FromFile(img);
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        bmp.SetResolution(80, 60);

        Graphics gfx = Graphics.FromImage(bmp);
        gfx.SmoothingMode = SmoothingMode.HighQuality;
        gfx.CompositingQuality = CompositingQuality.HighQuality;
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gfx.DrawImage(image, new Rectangle(0, 0, width, height), ((image.Width / 2) - (width / 2)), ((image.Height / 2) - (height / 2)), width, height, GraphicsUnit.Pixel);
        bmp.Save(des);
        // Dispose to free up resources
        bmp.Dispose();
        image.Dispose();
        gfx.Dispose();
    }

    public static void CreateThumb(string src, string dest)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(src);
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        int[] realThumbDimensions = GetProportionalThumbnailSize(srcWidth, srcHeight);
        int thumbWidth = realThumbDimensions[0];
        int thumbHeight = realThumbDimensions[1];
        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

        //Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image 
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

        //Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

        //Set the System.Drawing.Graphics object property InterpolationMode to High
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //Draw the original image into the target Graphics object scaling to the desired width and height
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

        bmp.Save(dest);

        bmp.Dispose();
        image.Dispose();
        gr.Dispose();
    }

    public static byte[] GetThumbnail(string realFileName)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(realFileName);
        int[] realThumbDimensions = GetProportionalThumbnailSize(originalImage.Width, originalImage.Height);

        System.Drawing.Image.GetThumbnailImageAbort myCallback =
            new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

        System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(
            realThumbDimensions[0],
            realThumbDimensions[1],
            myCallback,
            IntPtr.Zero);

        MemoryStream stream = new MemoryStream();
        thumbnail.Save(stream, originalImage.RawFormat);
        originalImage.Dispose();
        return stream.ToArray();
    }

    public static MemoryStream GetThumbnailStream(string realFileName)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(realFileName);
        int[] realThumbDimensions = GetProportionalThumbnailSize(originalImage.Width, originalImage.Height);

        System.Drawing.Image.GetThumbnailImageAbort myCallback =
            new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

        System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(
            realThumbDimensions[0],
            realThumbDimensions[1],
            myCallback,
            IntPtr.Zero);

        MemoryStream stream = new MemoryStream();
        thumbnail.Save(stream, originalImage.RawFormat);
        originalImage.Dispose();
        return stream;
    }

    public static bool ThumbnailCallback()
    {
        return false;
    }

    public static int[] GetProportionalThumbnailSize(int originalWidth, int originalHeight)
    {
        int realWidth;
        int realHeight;
        if (originalWidth < MAX_THUMBNAIL_WIDTH && originalHeight < MAX_THUMBNAIL_HEIGHT)
        {
            realWidth = originalWidth;
            realHeight = originalHeight;
        }
        else
        {
            realWidth = MAX_THUMBNAIL_WIDTH;
            realHeight = MAX_THUMBNAIL_HEIGHT;
        }
        if (originalWidth < MAX_THUMBNAIL_WIDTH && originalHeight < MAX_THUMBNAIL_HEIGHT)
        {
            return new int[] { realWidth, realHeight };
        }
        else
        {
            if (originalWidth > originalHeight)
            {
                realHeight = (int)Math.Round((decimal)(originalHeight * MAX_THUMBNAIL_WIDTH / originalWidth));
            }
            else
            {
                realWidth = (int)Math.Round((decimal)(originalWidth * MAX_THUMBNAIL_HEIGHT / originalHeight));
            }
            return new int[] { realWidth, realHeight };
        }
    }

    public static byte[] GetImage(string realFileName)
    {
        FileStream fs = File.OpenRead(realFileName);
        byte[] toReturn = new byte[fs.Length];
        fs.Read(toReturn, 0, (int)fs.Length);
        fs.Close();
        return toReturn;
    }

    public static string ResizeToNewImage(string Filename, string Location, int LimitX, int LimitY)
    {
        //declare var for size
        int limitX = LimitX;
        int limitY = LimitY;
        int x = 0;
        int y = 0;
        int newX = 0;
        int newY = 0;
        try
        {
            //load image
            string locationfile = Location + Filename;
            System.Drawing.Image objImage = System.Drawing.Image.FromFile(locationfile);
            //determine what are the new dimensions
            x = objImage.Width;
            y = objImage.Height;
            if (x > limitX || y > limitY)
            {
                if (x * 1.0 / y >= limitX * 1.0 / limitY)
                {
                    //shrink according to x
                    newX = limitX;
                    newY = Convert.ToInt32(y * (limitX * 1.0 / x));
                }
                else
                {
                    newY = limitY;
                    newX = Convert.ToInt32(x * (limitY * 1.0 / y));
                }
            }
            else
            {
                newX = x;
                newY = y;
            }

            //resize the image
            System.Drawing.Image.GetThumbnailImageAbort objCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            objImage = objImage.GetThumbnailImage(newX, newY, objCallback, IntPtr.Zero);

            //save image
            string[] temp = Filename.Split('.');
            string thumbs = temp[0] + "_s." + temp[1];
            string locationthumb = Location + thumbs;
            objImage.Save(locationthumb);
            return thumbs;
        }
        catch
        {
            return "";
        }
    }

    public static void Resize(string Filename, string Location, int LimitX, int LimitY)
    {
        //declare var for size
        int limitX = LimitX;
        int limitY = LimitY;
        int x = 0;
        int y = 0;
        int newX = 0;
        int newY = 0;
        try
        {
            //load image
            string LocationFile = Location + Filename;
            System.Drawing.Image objImage = System.Drawing.Image.FromFile(LocationFile);
            //determine what are the new dimensions
            x = objImage.Width;
            y = objImage.Height;
            if (limitY == 0) limitY = y;
            if (x > limitX || y > limitY)
            {
                if (x * 1.0 / y >= limitX * 1.0 / limitY)
                {
                    //shrink according to x
                    newX = limitX;
                    newY = Convert.ToInt32(y * (limitX * 1.0 / x));
                }
                else
                {
                    newY = limitY;
                    newX = Convert.ToInt32(x * (limitY * 1.0 / y));
                }
            }
            else
            {
                newX = x;
                newY = y;
            }

            //resize the image
            System.Drawing.Image.GetThumbnailImageAbort objCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            objImage = objImage.GetThumbnailImage(newX, newY, objCallback, IntPtr.Zero);

            //save image
            File.Delete(LocationFile);
            objImage.Save(LocationFile);
        }
        catch
        {
            //do whatever
        }
    }

    public static void WriteImageToFile(string FileName, byte[] ImageBytes)
    {
        FileStream fs = new FileStream(FileName, FileMode.Create);
        BinaryWriter w = new BinaryWriter(fs);
        try
        {
            w.Write(ImageBytes);
        }
        finally
        {
            fs.Close();
            w.Close();
        }
    }
}
